//
//  YLLoginUserViewModel.m
//  YLFinanceService
//
//  Created by she1323 on 2017/8/16.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLLoginUserViewModel.h"
#import "WHProgressHUD.h"
#import <YYText.h>
#import "NSString+Hash.h"
#import "WHNetworkTool.h"
#import "YLAccountTool.h"
#import "YLMyDefaultAddressViewModel.h"
#import "YLMineViewModel.h"

@interface YLLoginUserViewModel()

@property (nonatomic, strong) YLMineViewModel *mineViewModel;

@end

@implementation YLLoginUserViewModel

- (YLMineViewModel *)mineViewModel
{
    if (!_mineViewModel) {
        _mineViewModel = [[YLMineViewModel alloc] init];
    }
    return _mineViewModel;
}

- (instancetype)init
{
    if (self = [super init]) {
        [self setUp];
    }
    return self;
}

- (void)setUp
{
    _loginEnableSiganl = [RACSignal combineLatest:@[RACObserve(self, account), RACObserve(self, password)] reduce:^id _Nullable(NSString *account, NSString *password){
        return @(account.length && password.length);
    }];

    _otherLoginSubject = [RACSubject subject];
    _registerSubject = [RACSubject subject];
}

- (RACCommand *)loginCommand
{
    if (!_loginCommand) {
        __weak typeof(self) weakSelf = self;
        _loginCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                
                NSString *url = [NSString stringWithFormat:@"%@Account/Authenticate", YLURL];
                [WHProgressHUD showStatus:@"登录中..."];
                NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:2];
                params[@"TelCode"] = weakSelf.account;
                params[@"Password"] = [weakSelf.password md5String];
                
                [[WHNetworkTool shareInstance] sendPostWithUrl:url params:params success:^(id responseObject) {
                    if ([responseObject[@"httpCode"] integerValue] == 200) {
                        YLAccount *account = [YLAccount accountWithDict:responseObject[@"httpResult"]];
                        
                        account.phoneNumber = weakSelf.account;
                        [YLAccountTool saveAccount:account];
                        
                        [subscriber sendNext:nil];
                        
                        YLMyDefaultAddressViewModel *viewModel = [[YLMyDefaultAddressViewModel alloc] init];
                        [viewModel.requestCommand execute:nil];
                    } else {
                        [WHProgressHUD showError:responseObject[@"message"]];
                    }
                    
                    [subscriber sendCompleted];
                } fail:^(NSError *error) {
                    [subscriber sendCompleted];
                }];
                return nil;
            }];
        }];
    }
    return _loginCommand;
}

- (NSAttributedString *)attributedStringWithString:(NSString *)text
{
    __weak typeof(self) weakSelf = self;
    
    NSRange range1 = NSMakeRange(0, 5);
    
    NSRange range2 = NSMakeRange(16, 4);
    
    NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:text];
    
    att.yy_alignment = NSTextAlignmentCenter;
    
    [att yy_setTextHighlightRange:range1 color:[UIColor redColor] backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        [weakSelf.otherLoginSubject sendNext:nil];

    }];
    
    [att yy_setTextHighlightRange:range2 color:[UIColor redColor] backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        [weakSelf.registerSubject sendNext:nil];
    }];
    
    return att;
}

- (RACCommand *)otherLoginCommand
{
    if (!_otherLoginCommand) {
        __weak typeof(self) weakSelf = self;
        _otherLoginCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                
                NSString *url = [NSString stringWithFormat:@"%@Account/LoginByVerifyCode", YLURL];
                [WHProgressHUD showStatus:@"登录中..."];
                NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:2];
                params[@"TelCode"] = weakSelf.account;
                params[@"VerifyCode"] = weakSelf.password;
                
                [[WHNetworkTool shareInstance] sendPostWithUrl:url params:params success:^(id responseObject) {
                    if ([responseObject[@"httpCode"] integerValue] == 200) {
                                                YLAccount *account = [YLAccount accountWithDict:responseObject[@"httpResult"]];
                        
                                                account.phoneNumber = weakSelf.account;
                                                [YLAccountTool saveAccount:account];
                        
                                                [subscriber sendNext:nil];
                        
                        
                                                YLMyDefaultAddressViewModel *viewModel = [[YLMyDefaultAddressViewModel alloc] init];
                                                [viewModel.requestCommand execute:nil];
                    } else {
                        [WHProgressHUD showError:responseObject[@"message"]];
                    }
                    [subscriber sendCompleted];
                    
                    
                } fail:^(NSError *error) {
                    [subscriber sendCompleted];
                }];
                   
                return nil;
            }];
        }];
    }
    return _otherLoginCommand;
}
@end
